package com.apsfc.controller;

import com.apsfc.po.Type;
import com.apsfc.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 灯灯
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    @Qualifier("typeService")
    private TypeService typeService;

    //访问main.jsp
    @RequestMapping("/tomain")
    public String toTypeAdd(){
        return "admin/main";
    }

    @RequestMapping(value="/TypeAdd",method = RequestMethod.POST)
    public String TypeAdd(String name){
        if(name.equals("")){
            return "admin/type_add";
        }else {
            Type type = new Type();
            type.setName(name);
            typeService.TypeAdd(type);
            return "admin/type_add";
        }
    }

    @RequestMapping("/type")
    public String getAllType(Model model){
        List<Type> types = typeService.getAllType();
        model.addAttribute("types", types);
        return "admin/type";
    }

    @RequestMapping("/toTypeUpdate/{id}")
    public ModelAndView toTypeUpdate(@PathVariable("id") int id){
        Type type = typeService.findTypeById(id);
        return new ModelAndView("/admin/type_update","type", type);
    }

    @RequestMapping("/TypeUpdate")
    public ModelAndView TypeUpdate(Type type){
        Type type1 = type;
        if(type.getName().equals("")){
            return new ModelAndView("/admin/type_update","type1", type1);
        }else {
            typeService.TypeUpdate(type);
            List<Type> types = typeService.getAllType();
            return new ModelAndView("admin/type","types", types);
        }
    }

    @RequestMapping("/TypeDelete/{id}")
    public ModelAndView TypeDelete(@PathVariable("id") int id){
        typeService.TypeDelete(id);
        List<Type> types = typeService.getAllType();
        return new ModelAndView("admin/type","types", types);
    }

}
